import java.awt.event.*;
import java.awt.*;
import javax.swing.*;
import java.util.ArrayList;

/**
 * Class Checkerboard - write a description of the class here
 * 
 * @author (your name) 
 * @version (a version number)
 */
public class ShutTheBox extends JApplet implements MouseListener
{   
    public boolean rolled = false, gameOver = false;
    private Graphics canvas;

    int dice1=0;
    int dice2=0;
    ArrayList<NumberBlock> blockList = new ArrayList<NumberBlock>();
    /**
     * Called by the browser or applet viewer to inform this JApplet that it
     * has been loaded into the system. It is always called before the first 
     * time that the start method is called.
     */
    public void init()
    {
        // this is a workaround for a security conflict with some browsers
        // including some versions of Netscape & Internet Explorer which do 
        // not allow access to the AWT system event queue which JApplets do 
        // on startup to check access. May not be necessary with your browser. 
        JRootPane rootPane = this.getRootPane();    
        rootPane.putClientProperty("defeatSystemEventQueueCheck", Boolean.TRUE);

        // provide any initialisation necessary for your JApplet
        getContentPane().setBackground(Color.WHITE);
        addMouseListener(this);
    }

    /**
     * Called by the browser or applet viewer to inform this JApplet that it 
     * should start its execution. It is called after the init method and 
     * each time the JApplet is revisited in a Web page. 
     */
    public void start()
    {
    }

    /** 
     * Called by the browser or applet viewer to inform this JApplet that
     * it should stop its execution. It is called when the Web page that
     * contains this JApplet has been replaced by another page, and also
     * just before the JApplet is to be destroyed. 
     */
    public void stop()
    {
        // provide any code that needs to be run when page
        // is replaced by another page or before JApplet is destroyed 
    }

    public void mouseEntered(MouseEvent e) {}

    public void mouseExited(MouseEvent e) {}

    public void mousePressed(MouseEvent e) {}

    public void mouseReleased(MouseEvent e) {}

    public void mouseClicked(MouseEvent e)
    {
        int XPosition = e.getX(), YPosition = e.getY();
        if (XPosition < 0 || XPosition >= getWidth() || YPosition < 0 ||
        YPosition >= getWidth())
        {
            return;
        }
        //if roll has been clicked
        int dieSum = 0;
        if((XPosition >(getWidth()/2)-50) && (XPosition <(getWidth()/2)+70)
        && (YPosition>200) && (YPosition<275)){   

            dice1=(int)(Math.random()*6+1);
            dice2=(int)(Math.random()*6+1);
            dieSum = dice1+dice2;
            rolled = true;
            repaint();
        }

        int sum = 0;
        if (rolled){
            while (sum < dieSum) {
                for (NumberBlock nb : blockList)
                {
                    if (nb.contains(e.getX(), e.getY()))
                    {
                        sum += nb.getValue();
                        nb.clear(canvas);
                        blockList.remove(nb);
                    }
                }
            }
            rolled = false;
        }
        //if ((XPosition/50 + YPosition/50) % 2 == 0)
        //    JOptionPane.showMessageDialog(null, "RED");
    }

    /**
     * Paint method for applet.
     * 
     * @param  g   the Graphics object for this applet
     */
    public void paint(Graphics g)
    {      
        super.paint(g);
        this.canvas = g;
        drawStart(g);

        drawDice(g);
        drawNumberBlock(g);
    }

    public void drawNumberBlock(Graphics g){
        int k = 0;
        for (int i = 0; i < 9; i++){
            NumberBlock n = new NumberBlock(g, 25+i*50 , 50, i+1);
            blockList.add(n);
        }
    }

    public void drawStart(Graphics g){
        g.setColor(Color.BLUE);
        g.fillRect((getWidth()/2)-60, 200, 120, 75);
        g.setColor(Color.RED);
        g.setFont(new Font (Font.SERIF, 0, 40));
        g.drawString("ROLL", getWidth()/2 - 52, 250);
        g.setColor(Color.BLACK);
    }

    public void drawDice(Graphics g){
        if(dice1>0){
            int dice1x=50;
            int dice1y=185;
            int dice2x=getWidth()-175;
            int dice2y=185;
            int dotWidth=10;  
            int width = 125;
            g.drawRoundRect(dice1x,dice1y, width,width,25,25);
            g.drawRoundRect(dice2x,dice2y, width,width,25,25);

            g.clearRect(dice1x+5, dice1y+5, width-10, width-10);
            g.clearRect(dice2x+5, dice2y+5, width-10, width-10);
            //left die
            if(dice1==1||dice1==3||dice1==5){
                g.fillOval(dice1x+(width/2-dotWidth/2),dice1y+(width/2-dotWidth/2),dotWidth,dotWidth);
            }
            if (dice1==2||dice1==3||dice1==4||dice1==5||dice1==6){
                g.fillOval(dice1x+(width/4-dotWidth/2),dice1y+(width/4-dotWidth/2),dotWidth,dotWidth);
                g.fillOval(dice1x+(3*width/4-dotWidth/2),dice1y+(3*width/4-dotWidth/2),dotWidth,dotWidth);
            }
            if (dice1==4||dice1==5||dice1==6){
                g.fillOval(dice1x+(width/4-dotWidth/2),dice1y+(3*width/4-dotWidth/2),dotWidth,dotWidth);
                g.fillOval(dice1x+(3*width/4-dotWidth/2),dice1y+(width/4-dotWidth/2),dotWidth,dotWidth);
            }
            if (dice1==6){
                g.fillOval(dice1x+(width/4-dotWidth/2),dice1y+(width/2-dotWidth/2),dotWidth,dotWidth);
                g.fillOval(dice1x+(3*width/4-dotWidth/2),dice1y+(width/2-dotWidth/2),dotWidth,dotWidth);
            }
            //right die
            if(dice2==1||dice2==3||dice2==5){
                g.fillOval(dice2x+(width/2-dotWidth/2),dice2y+(width/2-dotWidth/2),dotWidth,dotWidth);
            }
            if (dice2==2||dice2==3||dice2==4||dice2==5||dice2==6){
                g.fillOval(dice2x+(width/4-dotWidth/2),dice2y+(width/4-dotWidth/2),dotWidth,dotWidth);
                g.fillOval(dice2x+(3*width/4-dotWidth/2),dice2y+(3*width/4-dotWidth/2),dotWidth,dotWidth);
            }
            if (dice2==4||dice2==5||dice2==6){
                g.fillOval(dice2x+(width/4-dotWidth/2),dice2y+(3*width/4-dotWidth/2),dotWidth,dotWidth);
                g.fillOval(dice2x+(3*width/4-dotWidth/2),dice2y+(width/4-dotWidth/2),dotWidth,dotWidth);
            }
            if (dice2==6){
                g.fillOval(dice2x+(width/4-dotWidth/2),dice2y+(width/2-dotWidth/2),dotWidth,dotWidth);
                g.fillOval(dice2x+(3*width/4-dotWidth/2),dice2y+(width/2-dotWidth/2),dotWidth,dotWidth);
            }
        }
        rolled = true;
    }

    /**
     * Called by the browser or applet viewer to inform this JApplet that it
     * is being reclaimed and that it should destroy any resources that it
     * has allocated. The stop method will always be called before destroy. 
     */
    public void destroy()
    {
        // provide code to be run when JApplet is about to be destroyed.
    }

    /**
     * Returns information about this applet. 
     * An applet should override this method to return a String containing 
     * information about the author, version, and copyright of the JApplet.
     *
     * @return a String representation of information about this JApplet
     */
    public String getAppletInfo()
    {
        // provide information about the applet
        return "Title:   \nAuthor:   \nA simple applet example description. ";
    }

    /**
     * Returns parameter information about this JApplet. 
     * Returns information about the parameters than are understood by this JApplet.
     * An applet should override this method to return an array of Strings 
     * describing these parameters. 
     * Each element of the array should be a set of three Strings containing 
     * the name, the type, and a description.
     *
     * @return a String[] representation of parameter information about this JApplet
     */
    public String[][] getParameterInfo()
    {
        // provide parameter information about the applet
        String paramInfo[][] = {
                {"firstParameter",    "1-10",    "description of first parameter"},
                {"status", "boolean", "description of second parameter"},
                {"images",   "url",     "description of third parameter"}
            };
        return paramInfo;
    }
}